<?php

/**
 * 登录限制
 * @author zlj <389843954@qq.com>
 */
namespace app\common\model;
use think\Model;
class Login extends Model
{

	/**
     * 记录错误登录次数
     * @param [string] $uid [用户id]
     * @return [bool] true
     */
    public function addLogin($uid){

    	$st = time();
    	$where["uid"] = $uid;
    	$res = $this->isexit($where);
    	if($res){
    		$otime = config("CHECK_OUT_TIME")*60;
    		if($st-$res["st"]<$otime*60){
    			//在一定时间内就 更改
	            $add["num"] = $res["num"]+1;
    		}else{
    			//大于一定时间 就重新赋值为1
    			$add["num"] = 1;
    			$add["st"] = $st;
    		}
	        $this->saveL($where,$add);
    	}else{
    		$add["uid"] = $uid;
    		$add["num"] = 1;
    		$add["st"] = $st;

    		self::insert($add);
    	}

    	return true;
    }

    /**
     * 验证是否通过
     * @param  [type] $uid [用户id]
     * @return [type]      [description]
     */
    public function checkLogin($uid){

    	$st = time();
    	$where["uid"] = $uid;
    	$res = $this->isexit($where);
    	if($res){
            $otime = config("CHECK_OUT_TIME")*60;
            $num = config("CHECK_NUM");
            //小于10分钟
            if($st-$res["st"]<$otime*60){
            	//大于5次
            	if($num<$res["num"]){
            		return false;
            	}
            }
    	}
    	return true;
    }

    /**
     * 查看是否存在
     * @param  [array] $where [查询条件]
     */
    public function isexit($where){
        
        $result = self::where($where)->find();
        if($result){
            $result = $result->getData();
        }

    	return $result;
    }

    /**
     * 删除记录
     * @param  [string] $uid [用户id]
     * @return [type]      [description]
     */
    public function delLogin($uid){
    	self::where(array("uid"=>$uid))->delete();
    	return true;
    }

    /**
	 * 修改操作
	 * @param  [array] $where [条件]
	 * @param  [array] $save  [修改内容]
	 * @return [type]
	 */
	public function saveL($where,$save){
		return self::where($where)->update($save);
	}
}